#include "newconnectiondialog.h"
#include "ui_newconnectiondialog.h"

#include <QMessageBox>
#include <QFileDialog>

#include "database.h"
#include "dbviewercommon.h"

NewConnectionDialog::NewConnectionDialog(DataBaseType type, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NewConnectionDialog)
{
    ui->setupUi(this);
    m_dbType = type;
    updateUiByDbType();
    connect(ui->comboBoxCheckType, SIGNAL(currentIndexChanged(int)), this, SLOT(onCheckTypeChanged(int)));
    connect(ui->pushButtonTest, &QPushButton::clicked, this, &NewConnectionDialog::onTestClicked);
    connect(ui->pushButtonConfirm, &QPushButton::clicked, this, &NewConnectionDialog::accept);
    connect(ui->pushButtonCancel, &QPushButton::clicked, this, &NewConnectionDialog::reject);
    connect(ui->toolButtonOpenFile, &QPushButton::clicked, this, &NewConnectionDialog::onOpenClicked);
}

NewConnectionDialog::~NewConnectionDialog()
{
    delete ui;
}

MSS NewConnectionDialog::getConnectionInfo()
{
    MSS info;
    QString connection = ui->lineEditConnection->text();
    QString host = ui->lineEditHost->text();
    QString port = ui->lineEditPort->text();
    QString username = ui->lineEditUsername->text();
    QString password = ui->lineEditPassword->text();
    QString path = ui->lineEditPath->text();
    info["dbName"] = "main";
    if (connection.isEmpty())
    {
        QMessageBox::warning(this, "提示", "连接名不能为空");
        return info;
    }
    if (m_dbType == DB_MYSQL)
    {
        if (host.isEmpty() || port.isEmpty() || username.isEmpty() || password.isEmpty())
        {
            QMessageBox::warning(this, "提示", "连接信息不能为空");
            return info;
        }
        info["host"] = host;
        info["port"] = port;
        info["username"] = username;
        info["dbName"] = "mysql";
        info["password"] = password;
        info["savePassword"] = ui->checkBoxSavePassword->isChecked() ? "1" : "0";
    }
    else if (m_dbType == DB_SQLSERVER)
    {
        if (host.isEmpty() || username.isEmpty() || password.isEmpty())
        {
            QMessageBox::warning(this, "提示", "连接信息不能为空");
            return info;
        }
        info["dbName"] = ui->lineEditInitDb->text();
        info["host"] = host;
        info["username"] = username;
        info["password"] = password;
        info["savePassword"] = ui->checkBoxSavePassword->isChecked() ? "1" : "0";
    }
    else if (m_dbType == DB_ACCESS)
    {
        if (path.isEmpty())
        {
            QMessageBox::warning(this, "提示", "连接信息不能为空");
            return info;
        }
        info["path"] = path;
    }
    else if (m_dbType == DB_SQLITE)
    {
        if (path.isEmpty())
        {
            QMessageBox::warning(this, "提示", "连接信息不能为空");
            return info;
        }
        info["path"] = path;
    }
    info["connection"] = connection;
    info["dir"] = ui->lineEditDir->text();
    info["autoConnect"] = ui->checkBoxAutoConnect->isChecked() ? "1" : "0";
    info["hasCode"] = ui->checkBoxHasCode->isChecked() ? "1" : "0";
    info["code"] = ui->lineEditCode->text();

    return info;
}

void NewConnectionDialog::onCheckTypeChanged(int index)
{
    ui->labelUsername->setVisible(index == 0);
    ui->lineEditUsername->setVisible(index == 0);
    ui->labelPassword->setVisible(index == 0);
    ui->lineEditPassword->setVisible(index == 0);
}

void NewConnectionDialog::onTestClicked()
{
    MSS info;
    info["host"] = ui->lineEditHost->text();
    info["port"] = ui->lineEditPort->text();
    info["username"] = ui->lineEditUsername->text();
    info["password"] = ui->lineEditPassword->text();
    info["path"] = ui->lineEditPath->text();
    Database db;
    QString msg = db.connectToDb(m_dbType, info) ? "连接成功" : "连接失败";
    QMessageBox::information(this, "提示", msg);
}

void NewConnectionDialog::onOpenClicked()
{
    QString filter = m_dbType == DB_ACCESS ? "access db(*.mdb)" : "sqlite db(*.db)";
    QString path = QFileDialog::getOpenFileName(this, "选择数据库文件", "", filter);
    if (path.isEmpty())
        return;

    ui->lineEditPath->setText(path);
}

void NewConnectionDialog::updateUiByDbType()
{
    // Mysql显示端口
    ui->labelPort->setVisible(m_dbType == DB_MYSQL);
    ui->lineEditPort->setVisible(m_dbType == DB_MYSQL);

    // MySql和SQL server显示主机、用户名、密码
    ui->labelHost->setVisible(m_dbType == DB_MYSQL || m_dbType == DB_SQLSERVER);
    ui->lineEditHost->setVisible(m_dbType == DB_MYSQL || m_dbType == DB_SQLSERVER);
    ui->labelUsername->setVisible(m_dbType == DB_MYSQL || m_dbType == DB_SQLSERVER);
    ui->lineEditUsername->setVisible(m_dbType == DB_MYSQL || m_dbType == DB_SQLSERVER);
    ui->labelPassword->setVisible(m_dbType == DB_MYSQL || m_dbType == DB_SQLSERVER);
    ui->lineEditPassword->setVisible(m_dbType == DB_MYSQL || m_dbType == DB_SQLSERVER);
    ui->checkBoxSavePassword->setVisible(m_dbType == DB_MYSQL || m_dbType == DB_SQLSERVER);

    // SQL server显示初始数据库、验证
    ui->labelInitDb->setVisible(m_dbType == DB_SQLSERVER);
    ui->lineEditInitDb->setVisible(m_dbType == DB_SQLSERVER);
    ui->labelCheckType->setVisible(m_dbType == DB_SQLSERVER);
    ui->comboBoxCheckType->setVisible(m_dbType == DB_SQLSERVER);
    ui->toolButtonSqlServer->setVisible(m_dbType == DB_SQLSERVER);

    // ACCESS和SQLITE显示数据库文件路径
    ui->labelPath->setVisible(m_dbType == DB_SQLITE || m_dbType == DB_ACCESS);
    ui->lineEditPath->setVisible(m_dbType == DB_SQLITE || m_dbType == DB_ACCESS);
    ui->toolButtonOpenFile->setVisible(m_dbType == DB_SQLITE || m_dbType == DB_ACCESS);

    // 根据数据库类型设置输入框默认值
    ui->lineEditConnection->setText(QString("%1_loacl").arg(DbViewerCommon::getDbTypeName(m_dbType)));
    ui->lineEditUsername->setText(m_dbType == DB_SQLSERVER ? "sa" : "root");
}
